/*
Copyright (C) 2012 John Tsiombikas <nuclear@member.fsf.org>,
                   Ioannis Makris <imak@imak.gr>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include "glory.h"
#include "gloin.h"
#include "wsys.h"
#include "log.h"

struct window *gloin_create_window(int xsz, int ysz, unsigned int mode)
{
	struct window *win;

	if(!(win = calloc(1, sizeof *win))) {
		gloin_log("gloin_create_window: failed to allocate window structure\n");
		return 0;
	}
	win->cb = &win->first_cbnode;

	if(gloin_wsys_create_window(win, xsz, ysz, mode) == -1) {
		free(win);
		return 0;
	}
	return win;
}


void gloin_destroy_window(struct window *win)
{
	/* first run the window-system specific window destruction code */
	gloin_wsys_destroy_window(win);

	/* cleanup the callback stack */
	while(win->cb) {
		void *tmp = win->cb;
		win->cb = win->cb->next;
		if(tmp != &win->first_cbnode) {
			free(tmp);
		}
	}
}

int gloin_mouse_id(struct window *win)
{
	return win->mouse_index;
}

void gloin_push_callbacks(struct window *win)
{
	struct cbnode *node;

	if(!(node = malloc(sizeof *node))) {
		gloin_log("gloin_push_callbacks: failed to allocate memory\n");
		return;
	}
	memcpy(node, win->cb, sizeof *node);

	node->next = win->cb;
	win->cb = node;
}

void gloin_pop_callbacks(struct window *win)
{
	struct cbnode *tmp;

	if(!win->cb->next) {
		gloin_log("gloin_pop_callbacks: stack underflow\n");
		return;
	}
	tmp = win->cb;
	win->cb = win->cb->next;
	free(tmp);

	gloin_wsys_set_callback_mask(win, win->cb->cbmask);
}

void gloin_clear_callbacks(struct window *win)
{
	memset(win->cb->func, 0, sizeof win->cb->func);
	memset(win->cb->cls, 0, sizeof win->cb->cls);

	gloin_wsys_set_callback_mask(win, 0);
}

void gloin_set_callback(struct window *win, int cb, void (*func)(), void *cls)
{
	win->cb->func[cb] = func;
	win->cb->cls[cb] = cls;

	if(func) {
		win->cb->cbmask |= 1 << cb;
	} else {
		win->cb->cbmask &= ~(1 << cb);
	}
	gloin_wsys_set_callback_mask(win, win->cb->cbmask);
}

void (*gloin_get_callback(struct window *win, int cb))()
{
	return win->cb->func[cb];
}

void *gloin_get_callback_data(struct window *win, int cb)
{
	return win->cb->cls[cb];
}

int gloin_have_idle(void)
{
	struct window *win;

	win = gloin_winlist();
	while(win) {
		if(gloin_get_callback(win, GLORY_IDLE)) {
			return 1;
		}
		win = win->next;
	}
	return 0;
}

void gloin_call_create(struct window *win)
{
	void (*func)();

	if((func = gloin_get_callback(win, GLORY_CREATE))) {
		gloin_set_active(win);
		func(gloin_get_callback_data(win, GLORY_CREATE));
	}

	if((func = gloin_get_callback(win, GLORY_RESHAPE))) {
		int xsz, ysz;
		gloin_wsys_get_size(win, &xsz, &ysz);

		gloin_set_active(win);
		func(xsz, ysz, gloin_get_callback_data(win, GLORY_RESHAPE));
	}
}

void gloin_call_idle(void)
{
	struct window *win = gloin_winlist();
	while(win) {
		void (*func)() = gloin_get_callback(win, GLORY_IDLE);
		if(func) {
			gloin_set_active(win);
			func(gloin_get_callback_data(win, GLORY_IDLE));
		}
		win = win->next;
	}
}
